using System;
using System.Data;
using System.Web.UI.WebControls;
using Framework.Library;
using Framework.Library.Components.Security;
using Framework.Library.Utilities;
using Framework.Modules.LinkSite.Library.Data;
using Framework.Modules.LinkSite.Library.DataAccess;

namespace Framework.Module.LinkSite.Admin
{
	public partial class LinkManager : AuthenticatedControl
	{
		int totalRecord = 0;
		protected void Page_Load(object sender, EventArgs e)
		{
			lblUpdateStatus.Text = string.Empty;
		}
		
		protected void dgLinks_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if(e.CommandName == "deletelink")
			{
				try
				{
					LinkDB.Delete(Convert.ToInt32(e.CommandArgument));
					lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
				}
				catch (Exception ex)
				{
					lblUpdateStatus.Text = ex.Message;
				}
				
			}
		}

		protected void dgLinks_RowEditing(object sender, GridViewEditEventArgs e)
		{
			dgLinks.EditIndex = e.NewEditIndex;
		}

		protected void dgLinks_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
		{
			dgLinks.EditIndex = -1;
		}

		protected void dgLinks_RowUpdating(object sender, GridViewUpdateEventArgs e)
		{
			GridViewRow Row = dgLinks.Rows[e.RowIndex];
			TextBox txtName = (TextBox)Row.FindControl("txtName");
			TextBox txtTarget = (TextBox)Row.FindControl("txtTarget");
			TextBox txtDescription = (TextBox)Row.FindControl("txtDescription");
			
			LinkInfo info = LinkDB.GetInfo(Convert.ToInt32(Row.Cells[1].Text));
			info.Link_Name = txtName.Text;
			info.Link_Target = txtTarget.Text;
			info.Link_Description = txtDescription.Text;
			try
			{
				LinkDB.Update(info);
				lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
			}
			catch (Exception ex)
			{
				lblUpdateStatus.Text = ex.Message;
			}
			dgLinks.EditIndex = -1;
		}
		protected void btnAddNew_Click(object sender, EventArgs e)
		{
			LinkInfo info = new LinkInfo();
			info.Link_Name = txtAddNew.Text;
			info.Link_Lang = AppEnv.GetLanguage();
			try
			{
				LinkDB.Insert(info);
				lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
			txtAddNew.Text = string.Empty;
		}
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			DataTable source = LinkDB.GetAll(AppEnv.GetLanguage());
			totalRecord = source.Rows.Count;
			dgLinks.DataSource = source;
			dgLinks.DataBind();
		}
		
		protected void dgLinks_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				e.Row.Cells[0].Text = Convert.ToString(e.Row.DataItemIndex + 1);
				DataRowView curData = (DataRowView)e.Row.DataItem;
				DropDownList dropIndex = (DropDownList)e.Row.FindControl("dropIndex");
				MiscUtility.FillIndex(dropIndex, 1, totalRecord + 1, Convert.ToInt32(curData["Link_Index"]));
				Button btnDeleteLink = (Button) e.Row.FindControl("btnDeleteLink");
				btnDeleteLink.Attributes.Add("onclick", MiscUtility.MSG_DELETE_CONFIRM);
			}
		}
	}
}